<template>
  <div>
    <el-dialog
      :title="dialogtitle"
      width="800px"
      class="icon-dialog dialog-form"
      :visible.sync="show"
      :before-close="closeForm"
      append-to-body
      @open="open"
    >
      <el-form
        ref="form"
        :size="size"
        :model="form"
        :rules="rules"
        :label-width="$store.getters.device !== 'mobile'?'16%':'90px'"
      >

        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="title" label-width="80px">
              <el-input v-model="form.title" auto-complete="off" clearable placeholder="请输入名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="分类" prop="cate" label-width="80px">
              <el-select v-model="form.cate" placeholder="请选择分类">
                <el-option v-for="option in cateArray" :key="option.id" :label="option.title" :value="option.id" ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="联系人" prop="contacts" label-width="80px">
              <el-input v-model="form.contacts" auto-complete="off" clearable placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="联系电话" prop="tel" label-width="80px">
              <el-input v-model="form.tel" auto-complete="off" clearable placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="地址" prop="addr" label-width="80px">
              <el-input v-model="form.addr" auto-complete="off" clearable placeholder="请输入地址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="分成" prop="divide" label-width="80px">
              <el-input v-model="form.divide" auto-complete="off" clearable placeholder="请输入分成" style="width:150px;" />
              %
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="推荐人" prop="referee_person" label-width="120px">
              <el-input v-model="form.referee_person" auto-complete="off" clearable placeholder="请输入推荐人手机号" style="width:200px;" />
           </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="推荐人手机号" prop="recommend" label-width="120px">
              <el-input v-model="form.recommend" auto-complete="off" clearable placeholder="请输入推荐人手机号" style="width:200px;" />
           </el-form-item>
          </el-col>
        </el-row>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="loading" type="primary" @click="submit">
          <span v-if="!loading">保 存</span>
          <span v-else>提 交 中...</span>
        </el-button>
        <el-button @click="closeForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'Storelevelupdate',
  components: {},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    opentype: {
      type: String,
      default: 'add'
    },
    size: {
      type: String,
      default: 'small'
    },
    info: {
      type: Object
    }
  },
  data() {
    return {
      dialogtitle: '',
      form: {},
      loading: false,
      rules: {
        title: [{
          required: true,
          message: '显示名称不能为空',
          trigger: 'blur'
        },{ validator: this.validateUsername, trigger: ['blur', 'change'] }],
        addr: [{
          required: true,
          message: '地址不能为空',
          trigger: 'blur'
        }],
        tel: [{
          required: true,
          message: '电话不能为空',
          trigger: 'blur'
        }],
        contacts: [{
          required: true,
          message: '联系人不能为空',
          trigger: 'blur'
        }],
      },
      cateArray: []
    }
  },
  watch: {
    show(val) {
    }
  },
  methods: {
    validateUsername(rule, value, callback) {
      // 异步检查是偶存在
      console.log(rule, value)
      this.$api.post('/supplier/vaildate', {title:value, id:this.form.id}).then(res => {
        if(res.code == 400){
          callback(new Error(res.msg));
        }else{
          callback();
        }
      });
    },
    open() {
      if (this.opentype == 'update') {
        this.dialogtitle = '修改'
        this.form = this.info
      } else {
        this.dialogtitle = '添加'
        this.form = { id: '', title: '', contacts: '', tel: '', addr: '' }
      }

      this.$api.post('/supplier/cate', {page:1, limit:10000}).then(res => {
        this.cateArray = res.data.item

      })




    },
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.$api.post('/supplier/update', this.form).then(res => {
            if (res.code == 2000) {
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.$emit('refesh_list')
              this.closeForm()
            }
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    closeForm() {
      this.$emit('update:show', false)
      this.loading = false
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
    }
  }
}
</script>

<style >

</style>
